<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Peserta_lulus extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->helper('flexigrid');
		$this->load->library('flexigrid');
		$this->load->library('session');	
		$this->load->model('peserta_lulus_model');		
		$this->load->model('peserta_model');		
		$this->load->model('batch_model');		
		$this->cek_login();
    }

	
	function cek_login(){
		$id_hak = $this->session->userdata('id_hak');
		if($id_hak < 1 || $id_hak > 2 || $id_hak == ''){
			redirect ('login');
		}
		
	}

	//load Grid
	function index(){
		$id_batch = $this->input->post('batch');
		if($id_batch == null)
		{
			if($this->session->userdata('id_batch') == null)
			{
				$id_batch = 0;
			}
			else
			{
				$id_batch = $this->session->userdata('id_batch');
			}
		}
		$this->daftar($id_batch);
	}
	
	
	function daftar($id_batch){
		$this->session->set_userdata('id_batch',$id_batch);
	
		$colModel['no'] = array('No',30,TRUE,'center',0);
		$colModel['Id_Peserta'] = array('No Peserta', 70, TRUE, 'center', 1);
		$colModel['Nama_Peserta'] = array('Nama Peserta',120,TRUE,'center',1);
		$colModel['Jenis_Kelamin'] = array('Jenis Kelamin',75,TRUE,'center',0);
		$colModel['Tempat_Lahir'] = array('Tempat Lahir',75,TRUE,'center',1);
		$colModel['No_HP'] = array('No HP',75,TRUE,'center',1);
		$colModel['Pekerjaan'] = array('Pekerjaan',100,TRUE,'center',1);
		$colModel['Jenis_Usaha'] = array('Jenis Usaha',120,TRUE,'center',1);
		$colModel['Omset'] = array('Omset',75,TRUE,'left',0);
		$colModel['Edit'] = array('Edit',30,TRUE,'center',0);
		
		//get nama batch
		$nama_batch = '-';
		$data_batch = $this->batch_model->selectone($id_batch);
		foreach ($data_batch->result() as $row){
			$nama_batch = $row->NAMA_BATCH;
		}
		
		$gridParams = array(
		'width' => 'auto',
		'height' => 300,
		'rp' => 10,
		'rpOptions' => '[5,10,15,20,25,40]',
		'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Peserta Lulus '.$nama_batch,
		'showTableToggleBtn' => true
		);
		
		$buttons[] = array('Generate Excel','excel','spt_js');
                $buttons[] = array('separator');
                $buttons[] = array('Hapus','delete','spt_js');
		
		// mengambil data dari file controler ajax pada method grid_berkas
		$grid_js = build_grid_js('flex1',site_url("peserta/peserta_lulus/grid_peserta_lulus/".$id_batch),$colModel,'Nama_Peserta','asc',$gridParams,$buttons);
		
		
		//additional JS
		$data['added_js'] = 
		"<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='".base_url()."index.php/peserta/peserta/form_peserta'; 
			}
			
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data peserta?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '".base_url()."index.php/peserta/peserta/delete"."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
                         
                          if (com=='Generate Excel')
                                {
                                    location.href='" . base_url() . "index.php/peserta/peserta_lulus/generate_excel/" . $id_batch . "'; 
                                }
				
		} 
		</script>
		";
		
		
		$data_grid['js_grid'] = $grid_js;
		
		//get data batch
                $data['id_batch'] = $id_batch;
                // if($this->session->userdata('id_hak')==1){
                    // $data['batch'] = $this->batch_model->get_all_batch();
                // }
                // else{
                    $data['batch'] = $this->batch_model->get_batch($this->session->userdata('id_lisensi'));
                //}
		
		//remdering view
		$data['title'] = 'Daftar Peserta Lulus';
		$this->template->write_view('header', 'template/header',$data);
		$this->template->write_view('content', 'peserta/grid_peserta_lulus',$data_grid);
		$this->template->write_view('footer', 'template/footer',$data);
		$this->template->render();	
	}

	function grid_peserta_lulus($id_batch){
			$valid_fields = array('Id_Peserta','Nama_Peserta','Jenis_Kelamin','Tempat_Lahir','No_HP','Pekerjaan','Jenis_Usaha','Omset','Status');
			$this->flexigrid->validate_post('Nama_Peserta','asc',$valid_fields);
			$records = $this->peserta_lulus_model->get_data_peserta($id_batch);
			$this->output->set_header($this->config->item('json_header'));

			$no = 0;
			foreach ($records['records']->result() as $row){
				$record_items[] = array(
					$row->ID_PESERTA,
					$no = $no+1,
					$row->ID_PESERTA,
					$row->NAMA_PESERTA,
					$row->JENIS_KELAMIN,
					$row->TEMPAT_LAHIR,
					$row->NO_HP,
					$row->PEKERJAAN,
					$row->JENIS_USAHA,
					'Rp. '.$row->OMSET.',-',
					'<a href=\'' . base_url() . 'index.php/peserta/peserta/edit/' . $row->ID_PESERTA . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/edit.png\'></a> '
				);
			}

		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
		else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
		}
	
	public function detail($id)
	{
		$record_peserta = $this->peserta_model->selectone($id);
		foreach ($record_peserta->result() as $peserta){
			$data['id_peserta'] = $peserta->ID_PESERTA;
			$data['nama_peserta'] = $peserta->NAMA_PESERTA;
			$data['nama_panggilan'] = $peserta->NAMA_PANGGILAN;
			$data['jenis_kelamin'] = $peserta->JENIS_KELAMIN;
			$data['tempat_lahir'] = $peserta->TEMPAT_LAHIR;
			$data['tanggal'] = $peserta->TANGGAL;
			$data['bulan'] = $peserta->BULAN;
			$data['tahun'] = $peserta->TAHUN;
			$data['nama_usaha'] = $peserta->NAMA_USAHA;
			$data['jenis_usaha'] = $peserta->JENIS_USAHA;
			$data['omset'] = $peserta->OMSET;
			$data['pekerjaan'] = $peserta->PEKERJAAN;
			$data['no_hp'] = $peserta->NO_HP;
			$data['no_darurat'] = $peserta->NO_DARURAT;
			$data['hub_darurat'] = $peserta->HUB_DARURAT;
			$data['email'] = $peserta->EMAIL;
			$data['twitter'] = $peserta->TWITTER;
			$data['ukuran_baju'] = $peserta->UKURAN_BAJU;
			$data['alamat'] = $peserta->ALAMAT;
			$data['kode_pos'] = $peserta->KODE_POS;
			$data['kota'] = $peserta->KOTA;
			$data['referensi'] = $peserta->REFERENSI;
			$data['no_transfer'] = $peserta->NO_TRANSFER;
			$data['tanggal_pembayaran'] = $peserta->TANGGAL_PEMBAYARAN;
			$data['promo'] = $peserta->PROMO;
			$data['informasi'] = $peserta->INFORMASI;
			$data['id_batch'] = $peserta->ID_BATCH;
		}
		
		//get data batch
		$id=1;
		$data['batch'] = $this->batch_model->get_batch($id);
		
		
		$data['status'] = 'preview';
		$data['title'] = 'Tambah Peserta';
		$this->template->write_view('header', 'template/header',$data);
		$this->template->write_view('content', 'peserta/form_peserta_lulus',$data);
		$this->template->write_view('footer', 'template/footer',$data);
		$this->template->render();
	}
        
        public function generate_excel($id_batch){
            //get nama batch
            $nama_batch = '-';
            $data_batch = $this->batch_model->selectone($id_batch);
            foreach ($data_batch->result() as $row) {
                $nama_batch = $row->NAMA_BATCH;
            }

            $data['nama_batch'] = $nama_batch;
            $data['peserta_lulus'] = $this->peserta_lulus_model->generate_excel($id_batch);
            $this->load->view('peserta/peserta_lulus_xls',$data);
        }

}

/* End of file pegawai.php */
/* Location: ./application/controllers/pegawai.php */